package com.rxyb.security.service.impl;

import cn.hutool.json.JSONUtil;
import cn.rxyb.core.respone.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rxyb.security.db.po.SysMenuResouce;
import com.rxyb.security.db.dao.SysMenuResouceMapper;
import com.rxyb.security.gate.model.AddMenuRoleReq;
import com.rxyb.security.gate.model.AddMenuRoleRsp;
import com.rxyb.security.gate.model.MenuListReq;
import com.rxyb.security.gate.model.MenuListRsp;
import com.rxyb.security.service.SysMenuResouceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxyb.security.service.logic.SysMenuResourceLogic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YJH
 * @since 2020-07-07
 */
@Service
@Slf4j
public class SysMenuResouceServiceImpl extends ServiceImpl<SysMenuResouceMapper, SysMenuResouce> implements SysMenuResouceService {

    private final SysMenuResouceMapper sysMenuResouceMapper;

    private final SysMenuResourceLogic sysMenuResourceLogic;

    public SysMenuResouceServiceImpl(SysMenuResouceMapper sysMenuResouceMapper, SysMenuResourceLogic sysMenuResourceLogic) {
        this.sysMenuResouceMapper = sysMenuResouceMapper;
        this.sysMenuResourceLogic = sysMenuResourceLogic;
    }

    @Override
    public R<MenuListRsp> menuList(MenuListReq req, MenuListRsp rsp) {
        log.info("sysMenu--->service--->menuList 请求参数[{}]", JSONUtil.toJsonStr(req));
        IPage<SysMenuResouce> sysMenuResouceIPage = sysMenuResouceMapper.selectPage(new Page<>(req.getCurrentPage(), req.getPageSize()), new QueryWrapper<>());
        rsp.setTotalCount(sysMenuResouceIPage.getTotal());
        rsp.setTotalPages(sysMenuResouceIPage.getPages());
        rsp.setBeans(sysMenuResouceIPage.getRecords().stream().map(e -> sysMenuResourceLogic.dbToModel(e)).collect(Collectors.toList()));
        return R.ok(rsp);
    }


    @Override
    public R<AddMenuRoleRsp> addMenuRole(AddMenuRoleReq req, AddMenuRoleRsp rsp) {
        log.info("sysMenu--->service--->addMenuRole 请求参数[{}]", JSONUtil.toJsonStr(req));
        SysMenuResouce resouce = sysMenuResourceLogic.modelToDb(req);
        sysMenuResouceMapper.insert(resouce);
        return R.ok(rsp);
    }
}
